<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-alert title="日志由系统根据任务自动生成，若当天因为请假或者其他原因未进行该任务的相关工作，请选择日志类型为请假或空闲。" show-icon type="warning" :closable="false">
          </el-alert>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-alert title="当日时间占比填写范围为【0-1】，日志占比关联工作量统计，请准确填写。" show-icon type="warning" :closable="false">
            <!-- 当天所有日志占比之和不能超过【1】 -->
          </el-alert>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="日志类型" prop="logtype" :rules="rules.validateRequire">
            <el-select v-model="model.logtype" filterable clearable @change="fuzhi">
              <el-option v-for="item in codeModel.logtypelist" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="当日时间占比" prop="timetroportion" :rules="rules.validateRequire">
            <el-input-number v-model="model.timetroportion" controls-position="right" :step="1" :min="0" :max="1" :precision="1" :value="0" style="width:100%" :disabled="isBianji"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务完成情况" prop="finishexplain" :rules="rules.validateRequire">
            <el-input v-model="model.finishexplain" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="center"><span>【以下信息为只读信息】</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>1、【任务】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="model.name" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmTaskRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务类型" prop="type">
            <el-input v-model="model.disptype" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务内容" prop="content">
            <el-input v-model="model.content" type="textarea" :rows="2" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>2、【工作日志】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <!-- <el-row>
                <el-col :span="24">
                    <el-form-item label="任务完成情况" prop="finishexplain">
                        <el-input v-model="model.finishexplain" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid">
            <el-input v-model="model.dispprojectid" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作日期" prop="finishtime">
            <el-date-picker v-model="model.finishtime" type="date" placeholder="完成日期" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="完成人" prop="username">
            <el-input v-model="model.username" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="完成人工号" prop="usercode">
            <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
                <el-col :span="12">
                    <el-form-item label="是否项目负责人" prop="ispm">
                        <el-select v-model="model.ispm" filterable clearable disabled>
                            <el-option v-for="item in codeModel.ispmList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否任务负责人" prop="istaskleader">
                        <el-select v-model="model.istaskleader" filterable clearable>
                            <el-option v-for="item in codeModel.istaskleaderList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交日期" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="date" placeholder="提交日期" value-format="yyyy-MM-dd" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmTaskLog" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <tl-attachment sourceType="PmTaskLog" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-task-ref ref="pmTaskRef" @confirm="getpmTaskRefData" :isSingleSelect="true"></tlpm-task-ref>
    <tlpm-team-ref ref="pmTeamRef" @confirm="getpmTeamRefData" :isSingleSelect="true"></tlpm-team-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmTaskRef from '@/views/ProjectManage/pmTask/refList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmTaskRef, tlpmTeamRef },
  data() {
    return {
      isBianji: false,
      moduleTitle: '任务工作日志',
      urlPrefix: '/pm/taskLog/',
      url: {
        // 额外的url
        editLog: '/pm/taskLog/updateLog'
      },
      // 任务自评进度
      other: {}
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
          this.$set(this.model, 'dispflowid', this.codeModel.flowidList[0].name)
        }
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'orgcode', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'finishtime', new Date())
      }
    },
    fuzhi(code) {
      if (code === '2' || code === '3') {
        this.model.timetroportion = 0
        this.isBianji = true
      }
      if (code === '1') {
        this.isBianji = false
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 打开任务名称参照 */
    openpmTaskRefDialog() {
      this.$refs.pmTaskRef.visible = true
    },
    /** 获取任务名称参数数据 */
    getpmTaskRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'taskid', selectionRow.id)
      this.$set(this.model, 'name', selectionRow.name)
      this.$set(this.model, 'type', selectionRow.tasktype)
      this.$set(this.model, 'disptype', selectionRow.disptasktype)
      this.$set(this.model, 'content', selectionRow.explain)
      this.$set(this.model, 'projectid', selectionRow.projectid)
    },
    /** 打开完成人姓名参照 */
    openpmTeamRefDialog() {
      this.$refs.pmTeamRef.visible = true
    },
    /** 获取完成人姓名参数数据 */
    getpmTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'username', selectionRow.username)
      this.$set(this.model, 'usercode', selectionRow.usercode)
      this.$set(this.model, 'orgcode', selectionRow.orgcode)
      this.$set(this.model, 'orgname', selectionRow.orgname)
      this.$set(this.model, 'deptcode', selectionRow.deptcode)
      this.$set(this.model, 'deptname', selectionRow.deptname)
    },
    saveCourierInfo(resolve, reject) {
      const { id, finishexplain, timetroportion, logtype } = this.model
      if (!finishexplain) {
        this.$tap.alert('请填写任务完成情况')
        reject()
        return
      }
      if (!(0 <= timetroportion && timetroportion <= 1)) {
        this.$tap.alert('请填写当日时间占比')
        reject()
        return
      }
      const param = {
        id,
        finishexplain,
        timetroportion,
        logtype
      }
      this.$tap.confirm('当前工作日志在当天的时间占比为【' + this.model.timetroportion + '】，是否确定？（日志占比关联工作量统计，请准确填写）')
        .then((_) => {
          putAction(this.url.editLog, param)
            .then(res => {
              if (res.success) {
                resolve()
              } else {
                this.$tap.alert(res.message, 'warning')
                reject()
              }
            })
            .catch(err => {
              this.$tap.alert('保存工作日志信息失败，请联系系统管理员', 'warning')
              reject()
            })
        })
        .catch((_) => {
          reject()
        })
    }
  }
}
</script>

